import React from 'react';
import { SystemControl } from '../components/System/SystemControl';
import { Map } from '../components/System/Map';
import { RobotStatus } from '../components/System/RobotStatus';
import { PopQueue } from '../components/System/PopQueue';
import { Statistics } from '../components/System/Statistics';
import { Sidebar } from '../components/layout/Sidebar';
import { useSystem } from '../hooks/useSystem';
import { Loading } from '../components/common/loading';
import { Toast } from '../components/common/Toast';
import { useToast } from '../hooks/useToast';
import { Link } from 'react-router-dom';
import { ArrowLeft } from 'lucide-react';

export function SystemPage() {
  const { toast, showToast, hideToast } = useToast();
  const {
    robotCount,
    idleRobotCount,
    robotInfoList,
    mapData,
    popQueue,
    completedOrders,
    aliveTime,
    averageConsumedTime,
    isLoading,
    error,
    startSystem,
    stopSystem
  } = useSystem();

  // const [statusCode, setStatusCode] = React.useState<string | null>(null); // 新增状态
  const [refreshSignal, setRefreshSignal] = React.useState<boolean>(false); // 用于触发 PopQueue 刷新

  const handleStart = (initRobotCount: number) => {
    startSystem(initRobotCount); // 启动时传递机器人数量
  };

  // 处理 stop 按钮点击事件
  const handleStop = () => {
    stopSystem();
    setRefreshSignal((prev) => !prev); // 切换 refreshSignal，触发 PopQueue 刷新
  };

  React.useEffect(() => {
    if (error) {
      showToast('系统状态获取失败，请稍后重试');
    }
  }, [error, showToast]);

  return (
    <div className="min-h-screen bg-gray-100">
      <Sidebar />
      <div className="ml-64 p-6">
        <div className="flex items-center gap-4 mb-6">
          <Link to="/" className="p-2 hover:bg-gray-200 rounded-full">
            <ArrowLeft size={24} />
          </Link>
          <h1 className="text-3xl font-bold">系统控制台</h1>
        </div>

        <div className="grid grid-cols-3 gap-6">
          <div className="col-span-2">
            {isLoading?
             <Loading /> : 
              <SystemControl
                onStart={handleStart}
                // onPause={pauseSystem}
                onStop={handleStop}
                aliveTime={aliveTime}
              />
            }
            
            <div className="mt-6">
              <Map 
                mapData={mapData}
                robotInfoList={robotInfoList}
              />
            </div>
          </div>

          <div className="space-y-6">
              <Statistics
                robots={robotInfoList}
                idleRobotCount={idleRobotCount}
                completedOrders={completedOrders}
                aliveTime={aliveTime}
                averageConsumedTime={averageConsumedTime}
              />
              <PopQueue 
                PopQueue={popQueue}
                refreshSignal={refreshSignal}
              />

              <RobotStatus
                robots={robotInfoList}
                onSelectRobot={(id) => console.log('Selected robot:', id)}
              />
          </div>
          
        </div>
      </div>
      {toast && (
        <Toast
          message={toast.message}
          type={toast.type}
          onClose={hideToast}
        />
      )}
    </div>
  );
}

